package com.reader.cms.controller.common;

import com.reader.cms.controller.BaseController;
import com.reader.cms.entity.common.ReaderComment;
import com.reader.cms.entity.common.ReaderCommentReply;
import com.reader.cms.service.common.IReaderCommentService;
import org.springframework.web.bind.annotation.*;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import com.reader.core.entity.ResultMessageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.reader.core.utils.ValidatorUtil;
import com.reader.core.validator.group.AddGroup;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author gumenghao
 * @description 功能描述
 * @date 2018-10-31
 */
@RestController
@RequestMapping("/api/readercomment")
public class ReaderCommentController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(ReaderCommentController.class);

    @Autowired
    public IReaderCommentService iReaderCommentService;


    /**
     * @param currentPage 当前页
     * @param size        每页记录数
     * @param docId       文章或音频ＩＤ
     * @return
     * @function 分页查询数据
     */
    @GetMapping("list")
    public ResultMessageInfo selectListPage(@RequestParam(value = "currentPage") int currentPage,
                                            @RequestParam(value = "size") int size,
                                            @RequestParam(value = "docId") String docId) {
        Page<ReaderComment> list = iReaderCommentService.selectListPage(currentPage, size, docId);
        return ResultMessageInfo.ofSuccess("查询成功！", list);
    }

    /**
     * @param readerComment 实体对象
     * @return
     * @function 添加新记录
     */
    @PostMapping("insert")
    public ResultMessageInfo insert(@RequestBody ReaderComment readerComment) {
        ValidatorUtil.validateEntity(readerComment, AddGroup.class);//非空验证

        Integer userId = this.getUserId();
        readerComment.setInsertDt(new Date());
        readerComment.setDelFlag(0);
        boolean resultValue = iReaderCommentService.insert(readerComment);
        if (!resultValue) {
            ResultMessageInfo.ofMessage("添加失败！");
        }
        return ResultMessageInfo.ofSuccess("添加成功！");
    }

    /**
     * @param readerComment 实体对象
     * @return
     * @function 评论更新（审核或置顶）
     */
    @PostMapping("update")
    @Transactional
    public ResultMessageInfo update(@RequestBody ReaderComment readerComment) {
        ValidatorUtil.validateEntity(readerComment, AddGroup.class);//非空验证
        Integer userId = this.getUserId();
        readerComment.setUpdtUserId(userId);
        readerComment.setUpdtDt(new Date());
        readerComment.setDelFlag(0);
        boolean resultValue = iReaderCommentService.updateById(readerComment);
        if (!resultValue) {
            return ResultMessageInfo.ofMessage("修改失败！");
        }
        return ResultMessageInfo.ofSuccess("修改成功！");
    }

    /**
     * @param readerCommentReply 实体对象
     * @return
     * @function 评论回复
     */
    @PostMapping("reply")
    @Transactional
    public ResultMessageInfo update(@RequestBody ReaderCommentReply readerCommentReply) {
        ValidatorUtil.validateEntity(readerCommentReply, AddGroup.class);//非空验证
        Integer userId = this.getUserId();
        readerCommentReply.setInsertDt(new Date());
        readerCommentReply.setInsertUserId(userId);
        readerCommentReply.setUpdtUserId(userId);
        boolean resultValue = iReaderCommentService.reply(readerCommentReply);
        if (!resultValue) {
            return ResultMessageInfo.ofMessage("回复成功！");
        }
        return ResultMessageInfo.ofSuccess("回复失败！");
    }

    /**
     * @param readerComment
     * @return
     * @function 评论删除
     */
    @PostMapping("delete")
    @Transactional
    public ResultMessageInfo deleteById(@RequestBody ReaderComment readerComment) {
        Integer userId = this.getUserId();
        readerComment.setUpdtDt(new Date());
        readerComment.setUpdtUserId(userId);
        readerComment.setDelFlag(1);
        boolean resultValue = iReaderCommentService.deleteById(readerComment.getId());
        if (!resultValue) {
            ResultMessageInfo.ofMessage("删除失败！");
        }
        return ResultMessageInfo.ofSuccess("删除成功！");

    }

    /**
     * @param data 键为ids值为整型数组的json格式
     * @return
     * @function 批量删除对象
     */
    @PostMapping("deleteIds")
    @Transactional
    public ResultMessageInfo deleteBatchIds(@RequestBody String data) {
        List<Long> ids = JSON.parseArray(JSON.parseObject(data).getString("ids"), Long.class);

        boolean resultValue = iReaderCommentService.deleteBatchIds(ids);
        if (!resultValue) {
            ResultMessageInfo.ofMessage("删除失败！");
        }
        return ResultMessageInfo.ofSuccess("删除成功！");
    }
}